<template>
  <div id="app">
    <div>
      <el-row>
        <el-col :span="3">
          <el-menu style="height: 660px; overflow-y: auto" :default-active="0">
            <el-menu-item
              v-for="(dict,index) in dictList"
              :key="dict.id"
              @click="getChildList(dict)"
              :index="index.toString()">
              <i class="el-icon-menu"></i>
              <span>{{ dict.name }}</span>
            </el-menu-item>
          </el-menu>
        </el-col>
        <el-col :span="21">
          <div style="margin-left: 15px; margin-bottom: 20px">
            <el-row>
              <el-col :span="3">
                <el-button type="primary" @click="addParent()">新增父节点</el-button>
              </el-col>
              <el-col :span="3">
                <el-button type="success" @click="addChild()">新增子节点</el-button>
              </el-col>
            </el-row>
          </div>
          <div style="height:580px; overflow-y: auto; margin-left: 15px">
            <el-table :data="dictTypeList" style="width: 100%"
                      :header-cell-style="{background:'#dde2e2', fontFamily:'Helvetica',fontSize:'14px'}">
              <el-table-column fixed prop="text" label="字典名称" width="300">
              </el-table-column>
              <el-table-column prop="enable" label="状态" width="250">
                <template slot-scope="scope">
                  <el-switch v-model="scope.row.enable" :active-value="1" :inactive-value="0" active-color="#32CD32"
                             inactive-color="#A9A9A9" @change="enableById(scope.row)">
                  </el-switch>
                </template>
              </el-table-column>
              <el-table-column prop="remark" label="备注" width="200">
              </el-table-column>
              <el-table-column prop="city" label="操作" width="300" align="center">
                <template slot-scope="scope">
                  <el-button icon="el-icon-edit" size="small" type="primary" @click="updateDict(scope.row)">编 辑
                  </el-button>
                  <el-button icon="el-icon-delete" size="small" type="danger" @click="deleteDict(scope.row)">删 除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="this.show" :title="this.title" style="text-align: left; padding-left: 25%; width: 50%;"
               @close="show = false">
      <el-form :model="rowDict" style="margin-left: 7%;">
        <el-form-item label="字典名称:" :label-width="formLabelWidth">
          <el-input v-model="rowDict.text" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态:" :label-width="formLabelWidth">
          <el-radio-group v-model="rowDict.enable">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注:" :label-width="formLabelWidth">
          <el-input v-model="rowDict.remark" autocomplete="off" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="this.showParent" :title="this.title" style="padding-left: 25%; width: 50%;"
               @close="showParent = false">
      <el-form :model="rowDict" style="padding: 5%;">
        <el-form-item label="字典名:" :label-width="formLabelWidth">
          <el-input v-model="rowDict.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典类型:" :label-width="formLabelWidth">
          <el-input v-model="rowDict.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注:" :label-width="formLabelWidth">
          <el-input v-model="rowDict.remark" autocomplete="off" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="showParent = false">取 消</el-button>
        <el-button type="primary" @click="saveParent()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'dict',
  data() {
    return {
      title: '',
      formLabelWidth: '100px',
      firstType: {},
      dictList: [],
      dictTypeList: [],
      temp: [],
      showParent: false,
      show: false,
      rowDict: {}
    }
  },
  created() {
    this.getDictList();
  },

  methods: {
    updateDict(param) {
      this.title = '编辑';
      this.show = true;
      this.rowDict = param;
    },
    addChild() {
      this.show = true;
      this.title = '新增子节点';
      this.rowDict = {"type": this.rowDict.type};
    },
    addParent() {
      this.showParent = true;
      this.title = '新增父节点';
      this.rowDict = {};
    },
    getDictList() {
      this.$axios.post("/dict-message/allType").then(res => {
        if (res.data.code === '200') {
          this.dictList = res.data.data;
          this.getChildList(this.dictList[0]);
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    enableById(param) {
      this.$axios.post("/dict-message/disableDict", {
        id: param.id,
        enable: param.enable
      }).then(res => {
        if (res.data.code === "200") {
          if (param.enable === 1) {
            this.$message({
              message: "启用字典成功",
              type: 'success'
            });
          }
          if (param.enable === 0) {
            this.$message({
              message: "禁用字典成功",
              type: 'success'
            });
          }

        } else {
          if (param.enable === 1) {
            this.$message.error("启用字典失败");
          }
          if (param.enable === 0) {
            this.$message.error("禁用字典失败");
          }
        }
      });
      this.firstType = param.type;
      this.getDictList();
    },
    saveOrUpdate() {
      this.show = false;
      this.$axios.post("/dict-message/saveOrUpdate", {
        id: this.rowDict.id,
        text: this.rowDict.text,
        type: this.firstType.type,
        enable: this.rowDict.enable,
        isChild: 1,
        parentId: this.firstType.id,
        name: this.firstType.name,
        remark: this.rowDict.remark
      }).then(res => {
        if (res.data.code === '200') {
          if (this.rowDict.id === 0) {
            this.$message({
              message: "新增成功",
              type: 'success'
            });
          } else {
            this.$message({
              message: "修改成功",
              type: 'success'
            });
          }
          this.getChildList(this.firstType);
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    getParent(param) {
      this.$axios.post("/dict-message/parent", {
        "type": param
      }).then(res => {
        this.firstType = res.data.data;
        this.getChildList(res.data.data.type);
      })
    },
    deleteDict(param) {
      this.$axios.post("/dict-message/delete", {
        "id": param.id
      }).then(res => {
        if (res.data.code === '200') {
            this.$message({
              message: "删除成功",
              type: 'success'
            });
          this.getChildList(this.firstType);
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    saveParent() {
      this.showParent = false;
      this.$axios.post("/dict-message/saveOrUpdate", {
        "value": 0,
        "type": this.rowDict.type,
        "enable": 1,
        "isChild": 0,
        "name": this.rowDict.name,
        "remark": this.rowDict.remark
      }).then(res => {
        if (res.data.code === '200') {
          this.$message({
            message: "新增成功",
            type: 'success'
          });
          this.getDictList();
        } else {
          this.$message({
            message: res.data.data,
            type: 'error'
          });
        }
      })
    },
    getChildList(param) {
      this.firstType = param;
      this.$axios.post("/dict-message/listByType", {
        "type": param.type
      }).then(res => {
        if (res.data.code === '200') {
          this.dictTypeList = res.data.data;
        } else {
          this.$message.error(res.data.message);
        }
      })
    }
  }
}
</script>

<style scoped>
#app {
  border-radius: 7px;
}
</style>
